
package com.sun.showcase.biz.mall.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.ShoppingCartDao;
import com.sun.showcase.biz.mall.service.GoodsService;
import com.sun.showcase.biz.mall.service.ShoppingCartService;
import com.sun.showcase.biz.mall.service.SkuService;
import com.sun.showcase.client.domain.mall.Goods;
import com.sun.showcase.client.domain.mall.ShoppingCart;
import com.sun.showcase.client.domain.mall.Sku;
import com.sun.showcase.client.query.mall.ShoppingCartQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("shoppingCartService")
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService{
	@Autowired
	private ShoppingCartDao shoppingCartDao;
	@Autowired
	private SkuService skuService;
	@Autowired
	private GoodsService goodsService;
	
	public void setShoppingCartDao(ShoppingCartDao dao) {
		this.shoppingCartDao = dao;
	}

	public DataGrid datagrid(ShoppingCartQuery shoppingCartQuery) {
		DataGrid j = new DataGrid();
		Pager<ShoppingCart> pager  = find(shoppingCartQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<ShoppingCartQuery> getQuerysFromEntitys(List<ShoppingCart> shoppingCarts) {
		List<ShoppingCartQuery> shoppingCartQuerys = new ArrayList<ShoppingCartQuery>();
		if (shoppingCarts != null && shoppingCarts.size() > 0) {
			for (ShoppingCart tb : shoppingCarts) {
				ShoppingCartQuery b = new ShoppingCartQuery();
				BeanUtils.copyProperties(tb, b);
				shoppingCartQuerys.add(b);
			}
		}
		return shoppingCartQuerys;
	}

	private Pager<ShoppingCart> find(ShoppingCartQuery shoppingCartQuery) {
		Page<ShoppingCart> page = PageHelper.startPage(shoppingCartQuery.getPage().intValue(), shoppingCartQuery.getRows().intValue());
		List<ShoppingCart> list = shoppingCartDao.findList(shoppingCartQuery);
		Pager<ShoppingCart> pager = new Pager<ShoppingCart>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public ShoppingCart add(ShoppingCartQuery shoppingCartQuery) {
		ShoppingCart t = new ShoppingCart();
		BeanUtils.copyProperties(shoppingCartQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateDate(new Date());
	    t.setModifiedDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		shoppingCartDao.save(t);
		shoppingCartQuery.setId(t.getId());
		return t;
		
	}

	public void update(ShoppingCartQuery shoppingCartQuery) {
		ShoppingCart t = shoppingCartDao.getById(shoppingCartQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(shoppingCartQuery, t);
		    t.setModifiedDate(new Date());
	    	shoppingCartDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(ShoppingCartQuery shoppingCartQuery) {
	    shoppingCartDao.updatePart(shoppingCartQuery);
	}

	public void delete(String[] ids) {
		shoppingCartDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		shoppingCartDao.deletePt(ids);
	}

	public ShoppingCart get(ShoppingCartQuery shoppingCartQuery) {
		return shoppingCartDao.getById(shoppingCartQuery.getId());
	}

	public ShoppingCart get(String id) {
		return shoppingCartDao.getById(id);
	}

	
	public List<ShoppingCartQuery> listAll(ShoppingCartQuery shoppingCartQuery) {
	    List<ShoppingCart> list = shoppingCartDao.findList(shoppingCartQuery);
		List<ShoppingCartQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public ExecuteResult<String> save(ShoppingCartQuery shoppingCartQuery) {
		ExecuteResult<String> res = new ExecuteResult<String>();
		if(StringUtils.isEmpty(shoppingCartQuery.getUserId())
				|| StringUtils.isEmpty(shoppingCartQuery.getUserName())) {
			res.addErrorMessage("无效用户！");
			return res;
		}
		if(StringUtils.isEmpty(shoppingCartQuery.getSkuId())) {
			res.addErrorMessage("无效商品！");
			return res;
		}
		Sku sku = skuService.get(shoppingCartQuery.getSkuId());
		if(sku == null) {
			res.addErrorMessage("无效商品！");
			return res;
		}
		Goods goods = goodsService.get(sku.getGoodsId());
		if(goods == null) {
			res.addErrorMessage("无效商品！");
			return res;
		}
		ShoppingCart cart = shoppingCartDao.existData(shoppingCartQuery);
		if(cart != null) {
			//已经存在 数量+1
			ShoppingCartQuery update = new ShoppingCartQuery();
			update.getSearchMap().put(StringConstant.ID, cart.getId());
			update.setBuyNum(cart.getBuyNum()+1);
			this.updatePart(update);
			res.setSuccessMessage("加入成功！");
			return res;
		}
		ShoppingCartQuery add = new ShoppingCartQuery();
		add.setUserId(shoppingCartQuery.getUserId());
		add.setUserName(shoppingCartQuery.getUserName());
		add.setShopId(goods.getShopId());
		add.setShopName(goods.getShopName());
		add.setGoodsId(goods.getId());
		add.setGoodsName(goods.getName());
		add.setMainPictureAddress(goods.getMainImg());
		add.setBuyNum(1);
		add.setSkuAttributeId(sku.getSkuAttributeId());
		add.setSkuId(sku.getId());
		add.setSkuJsonValue(sku.getSkuJsonValue());
		add.setSellPrice(sku.getSellPrice());
		add.setFreightId(goods.getFreightId());
		this.add(add);
		return res;
	}

	@Override
	public ExecuteResult<String> updateBuyNum(ShoppingCartQuery shoppingCartQuery) {
		ExecuteResult<String> res = new ExecuteResult<String>();
		if(StringUtils.isEmpty(shoppingCartQuery.getUserId())) {
			res.addErrorMessage("无效用户！");
			return res;
		}
		if(StringUtils.isEmpty(shoppingCartQuery.getId())
				|| shoppingCartQuery.getBuyNum() == null || shoppingCartQuery.getBuyNum()<=0) {
			res.addErrorMessage("无效更新！");
			return res;
		}
		ShoppingCartQuery update = new ShoppingCartQuery();
		update.getSearchMap().put(StringConstant.ID, shoppingCartQuery.getId());
		update.getSearchMap().put("user_id", shoppingCartQuery.getUserId());
		update.setBuyNum(shoppingCartQuery.getBuyNum());
		this.updatePart(update);
		res.setSuccessMessage("加入成功！");
		return res;
	}

	@Override
	public void deleteByUserId(String userId) {
		shoppingCartDao.deleteByUserId(userId);
	}

	@Override
	public void deleteById(String id) {
		shoppingCartDao.deleteById(id);
	}

	@Override
	public void updateBySkuId(ShoppingCartQuery shoppingCartQuery) {
		shoppingCartDao.updateBySkuId(shoppingCartQuery);
	}

	@Override
	public void deleteBySkuId(String skuId) {
		shoppingCartDao.deleteBySkuId(skuId);
	}

	@Override
	public void deleteByGoodsId(String goodsId) {
		shoppingCartDao.deleteByGoodsId(goodsId);
	}

	@Override
	public void updateFreightByGoodsId(String goodsId, String freightId) {
		shoppingCartDao.updateFreightByGoodsId(goodsId, freightId);
	}
	
	
}
